/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.kkkd.com/
 *
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseUIFragment;
import com.ouertech.android.agm.lib.ui.base.FragmentTab;
import com.ouertech.android.agm.lib.ui.base.FragmentTabAdapter;
import com.ouertech.android.agm.lib.ui.base.UIOptions;
import com.ouertech.android.agm.lib.ui.base.defaults.fragment.BaseTopFragment;
import com.simon.mengkou.R;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/2/29.
 * desc    : 首页
 */
public class CommunityFragment extends BaseTopFragment {
    //tablayout
    @Bind(R.id.base_id_appbar_tabs)
    TabLayout mTabLayout;
    //viewpager
    @Bind(R.id.base_id_content_viewpager)
    ViewPager mVpPager;

    //与appbar tab关联的fragments
    private List<FragmentTab> mAppTabs;


    @Override
    protected void initTop() {
        setTitle(R.string.community_title);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_community);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());

        //最新
        addAppTab(R.string.community_new, HomeNewFragment.class);
        //关注
        addAppTab(R.string.community_follow, HomeFollowFragment.class);

        mVpPager.setAdapter(new FragmentTabAdapter(getChildFragmentManager(),
                mAppTabs));
        mTabLayout.setupWithViewPager(mVpPager);
    }




    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls) {
        addAppTab(getString(txtRes), cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls) {
        addAppTab(title, cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     * @param args
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls, Bundle args) {
        addAppTab(getString(txtRes), cls, args);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     * @param args
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls, Bundle args) {
        if(mTabLayout != null && UtilString.isNotBlank(title)) {
            BaseUIFragment fragment = (BaseUIFragment) Fragment.instantiate(mActivity, cls.getName(), args);
            if(mAppTabs == null) {
                mAppTabs = new ArrayList<>(4);
            }

            FragmentTab tab = new FragmentTab();
            tab.setTitle(title);
            tab.setFragment(fragment);
            mAppTabs.add(tab);
        }
    }
}
